package com.party.web.web.controller.help;

import com.party.common.utils.StringUtils;
import com.party.core.model.help.Help;
import com.party.core.service.help.IHelpService;
import com.party.web.web.dto.AjaxResult;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * 帮助教程
 *
 * @author Administrator
 * @date 2017/7/20 0020 18:28
 */
@Controller
@RequestMapping("/help/help")
public class HelpController {

    @Autowired
    private IHelpService helpService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("list")
    public ModelAndView list(String id) {
        Help help = new Help();
        help.setType(0);
        List<Help> helpList = helpService.webListPage(help, new HashMap<String, Object>(), null);
        ModelAndView mv = new ModelAndView("help/helpList");
        mv.addObject("helpList", helpList);
        mv.addObject("id", id);
        return mv;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @RequestMapping("{id}/getDetail")
    public ModelAndView getDetail(@PathVariable("id") String id) {
        ModelAndView mv = new ModelAndView("help/helpDetail");
        Help help = helpService.get(id);
        if (StringUtils.isNotEmpty(help.getContent())) {
            String content = StringUtils.unescapeHtml4Video2Iframe(help.getContent());
            help.setContent(content);
        }
        if (StringUtils.isNotEmpty(help.getFrontContent())) {
            String content = StringUtils.unescapeHtml4Video2Iframe(help.getFrontContent());
            help.setFrontContent(content);
        }
        mv.addObject("currentHelp", help);
        String parentId = help.getParentId();
        if ("0".equals(parentId)) {
            Help pHelp = helpService.get(id);
            List<Help> childrens = helpService.list(new Help(pHelp.getId()));
            pHelp.setChildrens(childrens);
            mv.addObject("help", pHelp);
        } else if (!"0".equals(parentId)) {
            Help pHelp = helpService.get(parentId);
            List<Help> childrens = helpService.list(new Help(pHelp.getId()));
            pHelp.setChildrens(childrens);
            mv.addObject("help", pHelp);
        }
        return mv;
    }

    @RequestMapping("view")
    public ModelAndView view(String id){
        ModelAndView mv = new ModelAndView("help/techSupport");
        List<Help> titles = helpService.titleList(null);
        Help help = null;
        if(StringUtils.isNotBlank(id)){
            help = helpService.get(id);
            if (StringUtils.isNotEmpty(help.getContent())) {
                String content = StringUtils.unescapeHtml4Video2Iframe(help.getContent());
                help.setContent(content);
            }
        }else{
            if(titles.size() > 0){
                help = helpService.get(titles.get(0).getId());
                if (StringUtils.isNotEmpty(help.getContent())) {
                    String content = StringUtils.unescapeHtml4Video2Iframe(help.getContent());
                    help.setContent(content);
                }
            }else{
                help = new Help();
                help.setId("1");
                help.setTitle("内容正在完善中");
                help.setContent("<p>内容正在完善中</p>");
                titles.add(help);
            }
        }
        mv.addObject("titles",titles);
        mv.addObject("help",help);
        return mv;
    }

    @ResponseBody
    @RequestMapping("loadContent")
    public AjaxResult loadContent(String id){
        if(StringUtils.isBlank(id)){
            return AjaxResult.error("输入了非法数据");
        }
        Help help = helpService.get(id);
        if (StringUtils.isNotEmpty(help.getContent())) {
            String content = StringUtils.unescapeHtml4Video2Iframe(help.getContent());
            help.setContent(content);
        }
        if (StringUtils.isNotEmpty(help.getFrontContent())) {
            String content = StringUtils.unescapeHtml4Video2Iframe(help.getFrontContent());
            help.setFrontContent(content);
        }
        if(help == null){
            return AjaxResult.error("获取数据失败");
        }
        return AjaxResult.success(help);
    }
}
